import {reqAddCartOrUpdateSkuNum,reqDetailList} from '@/api/detail/index'
const state = {
    detailList: {}
};
const mutations = {
    GETDETAIL(state, detailList) {
        state.detailList = detailList;
    }
};
const actions = {
    // 加入购物车与修改商品个数的请求
    async addCartOrUpdate({state, commit, dispatch},{skuId,skuNum}){
        let res= await reqAddCartOrUpdateSkuNum(skuId,skuNum)
        // console.log(res);
        if(res.code== 200){
            return 'ok'
        }else{
            return Promise.reject();
        }
    },
    // 获取商品信息的
    async getDetail({ state, commit, dispatch }, skuId) {
        let result = await reqDetailList(skuId);
        if (result.code == 200) {
            commit('GETDETAIL', result.data);
        }
    },
};

const getters = {
    //skuInfo:商品详情的数据
    skuInfo() {
        //当服务器的数据未回来:state.detailList空对象,空对象打点一个没有的属性->undefined
        return state.detailList.skuInfo || {}
    },
};

export default {
  namespaced: "detail",
  state,
  actions,
  mutations,
  getters,
};
